package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.ProdBrandDto;
import com.yuke.cloud.service.pmc.dto.ProdIdsDto;
import com.yuke.cloud.service.pmc.entity.ProdBrand;
import com.yuke.cloud.service.pmc.entity.ProdCategory;
import com.yuke.cloud.service.pmc.service.ProdBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.service.pmc.service.ProdFactoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zys on 2018/09/28.
*/
@Slf4j
@Api(value = "WEB - ProdBrandController", tags = "处理ProdBrand相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/brand")
public class ProdBrandController extends BaseController {
    @Resource
    private ProdBrandService prodBrandService;
    @Resource
    private ProdFactoryService prodFactoryService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodBrand", value = "该数据记录的POJO") @RequestBody ProdBrand prodBrand) {
        prodBrandService.save(prodBrand);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        try {
            prodBrandService.deleteById(id);
        } catch (DataIntegrityViolationException e) {
            return WrapMapper.error("数据正在使用，无法删除");
        }
        return WrapMapper.ok();
    }

    @PostMapping("/ids")
    @ApiOperation(httpMethod = "POST", value = "根据ids删除多条记录")
    public Wrapper deleteByIds(@ApiParam(name = "factoryIds", value = "记录id") @RequestBody ProdIdsDto prodIdsDto) {
        try {
            prodBrandService.deleteByIds(prodIdsDto.getIds());
        } catch (DataIntegrityViolationException e) {
            return WrapMapper.error("数据正在使用，无法删除");
        }
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodBrand", value = "需要修改的内容") @RequestBody ProdBrand prodBrand) {
        prodBrandService.update(prodBrand);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdBrand> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ProdBrand prodBrand = prodBrandService.findById(id);
        prodBrand.setFactoryName(prodFactoryService.selectByKey(prodBrand.getFactoryId()).getFactoryName());
        return WrapMapper.ok(prodBrand);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdBrand>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdBrand> list = prodBrandService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/factory/{factoryId}")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<List<ProdBrand>> listByFactoryId(@ApiParam(name = "factoryId", value = "记录factoryId") @PathVariable Long factoryId) {
        ProdBrand prodBrand = new ProdBrand();
        prodBrand.setFactoryId(factoryId);
        List<ProdBrand> list = prodBrandService.select(prodBrand);
        return WrapMapper.ok(list);
    }

    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper ifExist(@ApiParam(name = "id", value = "记录id") @RequestBody ProdBrand prodBrand) {
        int count = prodBrandService.selectCount(prodBrand);
        return WrapMapper.ok(count);
    }

    @PostMapping("/condition")
    @ApiOperation(httpMethod = "POST", value = "根据factoryName获取具体信息")
    public Wrapper<PageInfo<ProdBrandDto>> getListByCondition(@ApiParam(name = "prodBrand", value = "记录prodBrand") @RequestBody(required = false) ProdBrand prodBrand,
                                      @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                      @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdBrandDto> prodBrandDtos = prodBrandService.getListByCondition(prodBrand);
        PageInfo pageInfo = new PageInfo(prodBrandDtos);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/enable/condition")
    @ApiOperation(httpMethod = "POST", value = "根据factoryId获取具体信息")
    public Wrapper<List<ProdBrandDto>> getEnableListByCondition(@ApiParam(name = "prodBrand", value = "记录prodBrand") @RequestBody(required = false) ProdBrand prodBrand) {
        List<ProdBrandDto> prodBrandDtos = prodBrandService.getEnableListByCondition(prodBrand);
        return WrapMapper.ok(prodBrandDtos);
    }

}
